package eyrolles.modele;

import eyrolles.mymodele.ModeleImpl;
import javax.sql.DataSource;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletContext;

import org.apache.struts.action.PlugIn;
import org.apache.struts.config.ModuleConfig;
import org.apache.struts.action.ActionServlet;

public class EyrollesPlugin implements PlugIn {

    public static final String PROPERTIES = "Model";
    private String modeleUrl = null;
    private String jndiUrl = null;

    public void init(ActionServlet servlet, ModuleConfig applicationConfig)
            throws javax.servlet.ServletException {


        System.out.println("---->Le plug-in démarre<----");
        Context initCtx;
        DataSource ds = null;
        try {
            initCtx = new InitialContext();
            ds = (DataSource) initCtx.lookup(jndiUrl);
        } catch (NamingException ex) {
            System.err.println("Error : plugin couldn't be iniated");
        }

        ModeleInterface modele = null;
        try {
            modele = (ModeleInterface) Factory.instantiates(modeleUrl);
        } catch (ClassNotFoundException ex) {
            System.err.println("Error : plugin couldn't be iniated");
        } catch (InstantiationException ex) {
            System.err.println("Error : plugin couldn't be iniated");
        } catch (IllegalAccessException ex) {
            System.err.println("Error : plugin couldn't be iniated");
        }
        modele.setDataSource(ds);
        ServletContext context = servlet.getServletContext();
        context.setAttribute(PROPERTIES, modele);

    }

    public String getModeleUrl() {
        return modeleUrl;
    }

    public void setModeleUrl(String f) {
        this.modeleUrl = f;
    }

    public String getJndiUrl() {
        return jndiUrl;
    }

    public void setJndiUrl(String jndiUrl) {
        this.jndiUrl = jndiUrl;
    }    
    
    
    public void destroy() {
    }
}
